package com.hishop.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hishop.common.utils.PageUtils;
import com.hishop.common.utils.Query;
import com.hishop.product.dao.AttrGroupDao;
import com.hishop.product.entity.AttrGroupEntity;
import com.hishop.product.service.AttrGroupService;
import com.hishop.product.vo.SpuItemAttrGroupVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Resource
    private AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (params.containsKey("key")) {
            String key = params.get("key").toString();
            wrapper.and((obj) -> {
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        IPage<AttrGroupEntity> page = new Page<>();
        if (catelogId == 0) {
            page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        } else {
            wrapper.eq("catelog_id",catelogId);
            page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        }
        return new PageUtils(page);
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //1、查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        AttrGroupDao baseMapper = this.getBaseMapper();
        List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);

        return vos;
    }

//    /**
//     * 根据键值查询信息
//     *
//     * @param key
//     * @return
//     */
//    @Override
//    public IPage<AttrGroupEntity> selectAttrGroupList(String key, Long catelogId,
//                                                     String pageIndex, String limit) {
//        return attrGroupDao.selectAttrGroupList(key, catelogId, pageIndex, limit);
//    }

//    @Override
//    public List<AttrGroupEntity> selectAttrGroupList2(String key) {
//        return attrGroupDao.selectAttrGroupList2(key);
//    }

}